<?php
/**
 * 定义h:tab控件
 * 
 * - Tab1
 * <code>
 * <h:tab 
 * 		action="xxx"
 * 		cond.action="xxx,yyy,zzz"
 * 		cond.param1="a,b,c"
 * 		selected.style="font-weight:bold"
 * 		param.a=1
 * 		param.b=2
 * >Tab1</h:tab>
 * </code>
 * 
 * - Tab2
 * <code>
 * <h:tab action="xxx" label="Tab2"/>
 * </code>
 * 
 * 可以自动继承当前浏览器端传递的参数：
 * <h:tab>
 * 	<h:attr name=url><h:uri/></h:attr>
 * </h:tab>
 * 
 * 支持的参数有：
 * - selected.* - <array> 被选中后的属性
 * - action - <string> 动作
 * - url - <string> URL，和action属性互斥(只能选其一)
 * - param.* - <array> 链接的参数
 * - cond.* - <array> 选中的条件，如 cond.action='log.add,log.modify'
 * - label - <string> 标签文字
 * - layout.tag - <string> 布局标签，默认为<a></a>，v1.1.2加入
 *
 * @package if
 * @subpackage module.ui
 * @since 1.1.0
 */
class UITab extends UIControl {
	protected $tpl = "html_tab";
	
	function _after_parse() {
		parent::_after_parse();
	}
	
	function _before_render() {
		$this->addReserveAttrs(array(
			"selected", "action", "url", "param", "cond", "label"
		));

		//构造URL
		if ($this->attr("url")) {
			$this->setAttr("href", $this->attr("url"));
		}
		elseif ($this->attr("action")) {
			if (!strstr($this->attr("action"), ".")) {
				$this->setAttr("action", __CONTROLLER__ . "." . $this->attr("action"));
			}
			$this->setAttr("href", $this->controller->path($this->attr("action")));
		}
		
		//构造URL Query String
		$params = $this->attr("param");
		if (is_array($params)) {
			$params = IURI::encodeQuery($params);
		}
		if ($params) {
			$href = $this->attr("href");
			if (strstr($href, "?")) {
				$href .= "&" . $params;
			}
			else {
				$href .= "?" . $params;
			}
			$this->setAttr("href", $href);
		}
		
		//布局
		$this->addReserveAttrs("layout");
		$tag = $this->attr("layout.tag");
		if (!$this->hasAttr("layout.tag")) {
			if ($this->parent()->hasAttr("tab.tag")) {
				$tag = $this->parent()->attr("tab.tag");
			}
			else {
				$tag = "a";
			}
		}
		if (strtolower($tag) != "a") {
			$href = $this->attr("href");
			$this->addReserveAttrs("href");
			$this->setAttr("onclick", "window.location='{$href}'");
		}
		$this->addTplVar("tag", $tag);
		
		//是否选中
		$selected = false;
		$cond = $this->attr("cond");
		$currentAction = __CONTROLLER__ . "." . __ACTION__;
		if (!is_array($cond)) {
			$cond = array( "action" => $this->attr("action") );
		}
		foreach ($cond as $type => $value) {
			if ($type == "action") {
				if (!is_array($value)) {
					$value = preg_split("/\s*,\s*/", $value);
				}
				foreach ($value as $_k => $_v) {
					$value[$_k] = strstr($_v, ".") ? $_v : __CONTROLLER__ . "." . $_v;
				}
				$selected = if_in_array($currentAction, $value);
			}
			else {
				$realValue = x($type);
				$selected = if_in_array($realValue, $value);
			}
		}
		
		//tabset
		$tabset = $this->parent("h:tabset");
		
		//选中后的属性
		if ($selected) {
			if ($tabset) {
				$selectedAttrs = $tabset->attr("selected");
				if (is_array($selectedAttrs)) {
					$this->setAttr($selectedAttrs);
				}
			}
			
			$selectedAttrs = $this->attr("selected");
			if (is_array($selectedAttrs)) {
				$this->setAttr($selectedAttrs);
			}
		}
		
		//tag
		$openTag = null;
		$closeTag = null;
		if ($tabset) {
			switch($tabset->tag()) {
				case "ul": $openTag = "<li>"; $closeTag = "</li>"; break;
				case "dl": $openTag = "<dt>"; $closeTag = "</dt>"; break;
			}
		}
		$this->addTplVar("openTag", $openTag);
		$this->addTplVar("closeTag", $closeTag . "\n");		
		
		//label
		$label = $this->attr("label");
		if ($label) {
			$this->addTplVar("label", $label);
		}
		else {
			$this->addTplVar("label", $this->content());
		}
	}
	
	function render() {
		return parent::render();
	}
}

?>